package com.yinhe.music.yhmusic.comment;

import android.view.View;

import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.collect.CollectPresenter;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.model.CommentList;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

import java.util.ArrayList;
import java.util.List;

public class CommentListPresenter extends CollectPresenter<BaseModel, ICommentListContract.ICommentListView>
        implements ICommentListContract.ICommentListPresenter {

    @Override
    public void getCommentList(int page, int pageSize, String commentType, int id) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getCommentList(id, commentType, page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(commentList -> {
                    getView().hideLoading();
                    setCommentListData(commentList, page);
                }, throwable -> {
                    getView().hideLoading();
                    getView().setLoadMoreFailUI();
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void comment(int id, String content, String commentType) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();
        addSubscription(mModel.comment(id, commentType, content)
                .compose(schedulerProvider.applySchedulers())
                .subscribe(baseInfoResponse -> {
                    if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                        getView().setCommentUI();
                        getView().showToast(baseInfoResponse.getMsg());
                    } else {
                        getView().showToast(baseInfoResponse.getMsg());
                    }
                    getView().hideLoading();
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);
                }));
    }

    private void setCommentListData(CommentList commentList, int page) {
        List<CommentSection> commentSectionList = new ArrayList<>();
        int pageNum = commentList.getPageNum();

        List<CommentList> hotList = commentList.getHotcomment();
        if (hotList != null) {
            if (page == 1 && !hotList.isEmpty()) {
                commentSectionList.add(new CommentSection(true, "热门评论"));
            }
            for (CommentList comment : hotList) {
                commentSectionList.add(new CommentSection(comment));
            }
        }

        List<CommentList> newList = commentList.getNewcomment();
        if (newList != null) {
            if (page == 1) {
                if (!newList.isEmpty()) {
                    getView().showEmptyUI(View.GONE);
                    commentSectionList.add(new CommentSection(true, "最新评论"));
                } else {
                    getView().showEmptyUI(View.VISIBLE);
                }
            }

            for (CommentList commentBean : newList) {
                commentSectionList.add(new CommentSection(commentBean));
            }
        }

        getView().setCommentListUI(commentSectionList, pageNum);
    }
}
